﻿Imports ACompany.OrderProcessing.Model
Imports ACompany.OrderProcessing.Services
Imports System.Text
Imports StructureMap

Public Class ProductPresenter

    Private view As IProductDetailView
    Private ProductService As IProductService
    Private SelectedProduct As Product
    Private mode As Mode

    Public Sub New(ByVal View As IProductDetailView)
        Me.New(View, ObjectFactory.GetInstance(Of IProductService)())
    End Sub

    Public Sub New(ByVal View As IProductDetailView, ByVal ProductService As IProductService)
        Me.view = View
        Me.ProductService = ProductService
    End Sub

    Public Sub Display()

        Dim Product As Product = GetSelectedProduct()

        clearInfoAndErrorMessages()
        setButtonText()
        setTitleText()

        If Product Is Nothing Then
            view.ErrorMessage(String.Format("Could not find details from Product with Id of {0}.", view.ProductId))
        Else
            UpdateView(Product)
        End If
    End Sub

    Private Sub setButtonText()
        If mode = mode.Add Then
            view.SaveButtonText = "Add Product"
        Else
            view.SaveButtonText = "Save Product"
        End If
    End Sub

    Private Sub setTitleText()
        If mode = mode.Add Then
            view.ViewTitle("Add Product")
        Else
            view.ViewTitle("Edit Product")
        End If
    End Sub

    Private Sub clearInfoAndErrorMessages()
        view.ErrorMessage(String.Empty)
        view.InfoMessage(String.Empty)
    End Sub

    Private Function GetSelectedProduct() As Product

        If SelectedProduct Is Nothing Then
            If view.ProductId = Guid.Empty Then
                ' Add mode
                SelectedProduct = New Product
                mode = Presentation.Mode.Add
            Else
                ' Edit mode
                SelectedProduct = Me.ProductService.FindBy(view.ProductId)
                mode = Presentation.Mode.Edit
            End If
        End If

        Return SelectedProduct

    End Function

    Private Sub UpdateView(ByVal SelectedProduct As Product)

        view.Name = SelectedProduct.Name
        view.Price = SelectedProduct.Price
        view.Stock = SelectedProduct.Stock
    End Sub

    Public Sub SaveProduct()

        Dim Product As Product = GetSelectedProduct()

        UpdateFromView(Product)
        clearInfoAndErrorMessages()

        If Product.GetBrokenRules.Count = 0 Then
            Try
                Me.ProductService.Save(Product)

                If mode = mode.Add Then
                    Me.view.InfoMessage(String.Format("Product '{0}' has been added", Product.Name))                   
                    ClearView()
                Else
                    Me.view.InfoMessage("Product has been saved")
                End If

            Catch ex As Exception

                Dim exceptionMessage As String = String.Format("A problem occurred while saving Product '{0}'. {1}{2}", Product.Name, Environment.NewLine, ex.Message)
                view.ErrorMessage(exceptionMessage)

            End Try
        Else
            DisplayErrorsIn(Product)
        End If

    End Sub

    Private Sub UpdateFromView(ByVal Product As Product)
        Product.Name = view.Name
        Product.Price = view.Price
        Product.Stock = view.Stock       
    End Sub

    Private Sub ClearView()
        view.Name = String.Empty
        view.Stock = 0
        view.Price = 0
    End Sub

    Private Sub DisplayErrorsIn(ByVal Product As Product)

        Dim sb As New StringBuilder

        sb.Append("There were some problems when trying to save this Product:")
        sb.Append(Environment.NewLine)
        sb.Append(Environment.NewLine)

        For Each BrokenRule As Infrastructure.BrokenRule In Product.GetBrokenRules
            sb.Append(BrokenRule.Rule)
            sb.Append(Environment.NewLine)
        Next

        view.ErrorMessage(sb.ToString)
    End Sub
End Class
